// pages/myself/myself.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showTopTips: false,
        grids: [0, 1, 2, 3, 4, 5, 6, 7, 8],
        money:0,
        isshow: false,
        islogin: false,
        showModalStatus: false,
        animationData: '',
        svrnum:0
    },
    onLoad: function(options) {
      //console.log(app.globalData.isLoginAuth);
        var that=this;
        if (app.globalData.isLoginAuth){
          this.setData({
              userInfo: app.globalData.userInfo,
              islogin: app.globalData.isLoginAuth
          });
        }
        else{
          this.showModal();
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function(options) {
      var that=this;
      if (1 == wx.getStorageSync('roleidentity')) {
        var identity = wx.getStorageSync('roleFlag');
        wx.request({
          url: app.globalData.hostUrl + 'member/applyForsvrer',
          data: {
            identity: identity,
            uid: wx.getStorageSync('userId')
          },
          methods: 'GET',
          header: {
            "Content-Type": "json"
          },
          success: function (res) {
            console.log(res);
            if (res.data.result) {
              that.setData({
                svrnum: res.data.result.lists.length
              });
            }
          }
        })
      }
      if (app.globalData.isLoginAuth) {
        this.getfansinfo();
      }
    },
   
    getlogin:function(){
      this.showModal()
    },
    getfansinfo:function(){
      var that = this;
      wx.request({
        url: app.globalData.hostUrl + 'member/getfansinfo',
        data: {
          id: wx.getStorageSync('userId')
        },
        success: function (res) {
         // console.log(res);
          var isshow=false;
          var roleflag='fans';
          var role = res.data.result.role;
          if (role == '教练' || role == '陪练'){
            roleflag ='teacher';
          }
          if (role == '技师') {
            roleflag = 'mechanic';
          }
          if (role == '销售员') {
            roleflag = 'saler';
          }
          if (role == '汽服员') {
            roleflag = 'carsvr';
          }
          wx.setStorageSync('roleFlag', roleflag);
          if (role == '教练' || role == '陪练' || role == '技师' || role == '销售员' || role == '汽服员'){
            isshow=true;
          }

          that.setData({
            money: res.data.result.money,
            isshow: isshow
          })
        }
      })
    },
  gotopage: function (e) {
    if (!this.data.islogin) {
      this.showModal();
      return false;
    }
    var gotourl = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: gotourl,
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else {
      wx.redirectTo({
        url: '../myself/myself',
      })
    }
  }
})